package com.turing.post.account.impl;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.account.util.OnlineTransferDetail;
import com.turing.post.account.util.OnlineTransferDetailDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

@Stateless(mappedName = "account.impl.OnlineTransferDetailDaoImpl")
@Remote
@Local
public class OnlineTransferDetailDaoImpl implements OnlineTransferDetailDao {

	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	@Override
	public OnlineTransferDetail getOnlineTransferDetailByLocalSeqNo(
			String localSeqNo) {
		return _em.find(OnlineTransferDetail.class, localSeqNo);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<OnlineTransferDetail> getOnlineTransferDetailList(
			PCPSWebInput webInput, String localSeqNo, String cardNo,
			String saleBranchNo, String startTime, String endTime,
			String tranType, String status, String checkStatus, Integer page) {
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			return null;
		}
		String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		String sql = "select t.* from OnlineTransferDetail t"
				+ " where t.issuecompanyno='" + issueCompanyNo + "'"
				+ " and (t.localseqno = ?1 or ?1 is null)"
				+ " and t.trantime >= to_date(?2,'yyyy-mm-dd hh24:mi:ss') "
				+ " and t.trantime <= to_date(?3,'yyyy-mm-dd hh24:mi:ss') "
				+ " and (t.trantype = ?4 or ?4 is null)"
				+ " and (t.status = ?5 or ?5 is null)"
				+ " and (t.checkstatus = ?6 or ?6 is null)"
				+ " and (t.cardno = ?7 or ?7 is null)"
				+ " order by t.trantime desc";
		Query query = _em.createNativeQuery(sql, OnlineTransferDetail.class)
				.setFirstResult(startPosition).setMaxResults(maxResult);
		query.setParameter(1, localSeqNo);
		query.setParameter(2, startTime + " 00:00:00");
		query.setParameter(3, endTime + " 23:59:59");
		query.setParameter(4, tranType);
		query.setParameter(5, status);
		query.setParameter(6, checkStatus);
		query.setParameter(7, cardNo);
		// 判断是查询全部还是选定机构
		sql += PCPSUtil.getSqlCond(webInput, saleBranchNo, "SaleBranchNo");

		return query.getResultList();
	}

	@Override
	public BigInteger getOnlineTransferDetailTotal(PCPSWebInput webInput,
			String localSeqNo, String cardNo, String saleBranchNo,
			String startTime, String endTime, String tranType, String status,
			String checkStatus) {
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			return null;
		}
		String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
		String sql = "select nvl(count(*),0) from OnlineTransferDetail t"
				+ " where t.issuecompanyno='" + issueCompanyNo + "'"
				+ " and (t.localseqno = ?1 or ?1 is null)"
				+ " and t.trantime >= to_date(?2,'yyyy-mm-dd hh24:mi:ss') "
				+ " and t.trantime <= to_date(?3,'yyyy-mm-dd hh24:mi:ss') "
				+ " and (t.trantype = ?4 or ?4 is null)"
				+ " and (t.status = ?5 or ?5 is null)"
				+ " and (t.checkstatus = ?6 or ?6 is null)"
				+ " and (t.cardno = ?7 or ?7 is null)"
				+ " order by t.trantime desc";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, localSeqNo);
		query.setParameter(2, startTime + " 00:00:00");
		query.setParameter(3, endTime + " 23:59:59");
		query.setParameter(4, tranType);
		query.setParameter(5, status);
		query.setParameter(6, checkStatus);
		query.setParameter(7, cardNo);
		// 判断是查询全部还是选定机构
		sql += PCPSUtil.getSqlCond(webInput, saleBranchNo, "SaleBranchNo");

		return new BigInteger(query.getSingleResult().toString());
	}

	@Override
	public void insert(OnlineTransferDetail otd)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, otd);
	}

	@Override
	public void update(OnlineTransferDetail otd)
			throws PCPSApplicationException {
		GeneralDao.update(_em, otd);
	}

	@Override
	@TransactionAttribute(javax.ejb.TransactionAttributeType.REQUIRES_NEW)
	public void updateOnlineTransferDetailStatus(String localSeqNo,
			String status, String rtnCode) throws PCPSApplicationException {
		OnlineTransferDetail otd = getOnlineTransferDetailByLocalSeqNo(localSeqNo);
		otd.setStatus(status);
		otd.setBankRespCode(rtnCode);
		update(otd);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> generateOnlineTransferDetail(String issueCompanyNo,
			Date tranDate) {
		String sql = "select BankManageId,Trandate,IssueCompanyNo,TranType,count(*),sum(Amount) "
				+ " where IssueCompanyNo = ?1 and Trandate = to_date(?2,'yyyy-mm-dd')"
				+ " from OnlineTransferDetail group by IssueCompanyNo, Trandate, BankManageId, TranType";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, issueCompanyNo);
		query.setParameter(2, PCPSUtil.getPackedYMDString2(tranDate));
		return query.getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public Object[] checkAccuntByTrandateAndTranType(
			String Trandate, String TranType) throws PCPSApplicationException {
		String sql = "select count(*),sum(amount) from OnlineTransferDetail otd " +
				"where otd.trantype=?1 and otd.status='1' and otd.trandate = to_date(?2,'yyyy-mm-dd')";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, TranType);
		query.setParameter(2, Trandate);
		List<Object[]> otdList = query.getResultList();
		if(otdList.size()<1)
			return null;
		else
			return otdList.get(0);
	}

	@Override
	public double checkAccuntDetailByTrandateAndTranType(String Trandate,
			String TranType, String CardNo) throws PCPSApplicationException {
		String sql ="select otd.amount from OnlineTransferDetail otd where where otd.trantype = ?1 " +
				"and otd.status = '1' and otd.trandate = to_date(?2, 'yyyy-mm-dd') and otd.cardno = ?3";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, TranType);
		query.setParameter(2, Trandate);
		query.setParameter(3, CardNo);
		return Double.valueOf(query.getSingleResult().toString());
	}

	@Override
	public void updateCheckAccuntSuccessDetail(String Trandate, String TranType)
			throws PCPSApplicationException {
		String sql = "update OnlineTransferDetail otd set otd.checkstatus='1' where otd.trantype=?1 and otd.status='1' " +
				"and otd.trandate = to_date(?2,'yyyy-mm-dd')";
		Query query = _em.createNamedQuery(sql);
		query.setParameter(1, TranType);
		query.setParameter(2, Trandate);
		query.executeUpdate();
	}

}
